<template>
  <div>
    <h1>添加车位</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
      <el-form-item label="车位编号" prop="carId">
        <el-input v-model="ruleForm.carId"></el-input>
      </el-form-item>
      <el-form-item label="车位名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="车位地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        carId:'',
        name:'',
        address:'',
        enable:'',
      },
      rules: {

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8081/pms/add-new';
          console.log('尝试添加车位……');
          console.log('请求路径为：' + url);
          console.log('请求参数为：' + this.ruleForm);
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('将ruleForm对象转换为FormData：');
          console.log(formData);
          this.axios
              .post(url,formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response);

            let responseData = response.data;
            if (responseData.state == 20000) {
              console.log('添加车位成功');
              this.$message({
                message: '添加车位成功！',
                type: 'success'
              });
              this.resetForm(formName);//还原页面
            } else {
              console.log(responseData.message);
              this.$notify.error({
                title: '添加车位失败',
                message: responseData.message
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

}
</script>

<style scoped>

</style>